<template>
  <div class="dialog">
    <el-dialog
      :visible.sync="dialogVisible"
      width="700px"
      append-to-body
      :modal="true"
      @close="resetAudit"
    >
      <span slot="title" class="dialog-title">{{ dialogTitle }}</span>
      <div class="dialogBody">
        <div class="title">
          <span>申编号：{{ params.bomApplyId }}</span>
          <span>申请人：{{ params.applyUserName }}</span>
          <span>申请日期：{{ params.applyDate }}</span>
        </div>
        <p class="purpose">申请用途：{{ params.purpose }}</p>
        <p class="clinicName">{{ params.clinicName }}</p>
        <el-divider></el-divider>
        <div class="showMaterila">
          <p>
            <el-radio-group v-model="pass" @change="changeHandlerTotal">
              <el-radio label="1">全部通过</el-radio>
              <el-radio label="2">全部未通过</el-radio>
            </el-radio-group>
          </p>
          <ul v-if="showList.length">
            <li v-for="item of showList" :key="item.bomId">
              <span class="showCount">{{ item.bomName }}</span>
              <img :src="item.imgUrl" alt="" width="60" height="40" @click="checkImg(item.imgUrl)" />
              <span class="showCount">介绍：{{ item.intro }}</span>
              <span class="showCount">{{ item.remaining }}套</span>
              <el-radio-group v-model="item[item.bomAuditStated]" class="radio-change" @change="changeHandler(item)">
                <el-radio label="02">通过</el-radio>
                <el-radio label="03">未通过</el-radio>
              </el-radio-group>
            </li>
          </ul>
          <el-input v-model="remake" type="textarea" rows="5" placeholder="未通过原因"></el-input>
          <el-dialog :visible.sync="preview" append-to-body :modal="true" title="图片查看">
            <img width="100%" :src="imgUrl" alt="" />
          </el-dialog>
        </div>
        <div class="btn">
          <el-button type="primary" @click="resetAudit">取消</el-button>
          <el-button type="primary" @click="submitForm">提交</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { parseTime } from '@/utils/index';
import { boomApplyAudit } from '@/api/bomApply';
export default {
  components: {},
  data() {
    return {
      dialogVisible: false,
      dialogTitle: '物料审核',
      remake: '', // 未通过原因
      params: {
        clinicName: null, // 公司名称
        applyUserName: null, // 申请人
        applyDate: null, // 申请时间
        purpose: null, // 申请用途
        bomApplyId: null // 申请编号
      },
      showList: [],
      preview: false,
      imgUrl: null,
      pass: 0 // 是否全部通过
    };
  },
  methods: {
    isShow(list, item) {
      for (const key in this.params) {
        this.params[key] = item[key];
      }
      this.params.applyDate = parseTime(item.applyDate, '{y}-{m}-{d}');
      const details = item.applyDetails;
      details.forEach(e => {
        list.forEach(x => {
          if (Number(e.bomId) === Number(x.bomId)) {
            x.bomAuditStated = 'bomAuditStat';
            this.showList.push(x);
            return;
          }
        });
      });
      this.dialogVisible = true;
    },
    checkImg(url) {
      this.imgUrl = url;
      this.preview = true;
    },
    // 单个通过 未通过
    changeHandler(item) {
      this.pass = 0;
    },
    // 全部通过未通过
    changeHandlerTotal(e) {
      if (Number(e) === 1) {
        this.showList.forEach((item) => {
          this.$set(item, 'bomAuditStat', '02');
        });
      } else {
        this.showList.forEach((item) => {
          this.$set(item, 'bomAuditStat', '03');
        });
      }
    },
    submitForm() {
      console.log(this.showList);
      const params = {
        remark: this.remake,
        bomApplyId: this.params.bomApplyId,
        bomAudits: []
      };
      this.showList.forEach(item => {
        if (item.bomAuditStat) {
          params.bomAudits.push({ bomId: Number(item.bomId), auditStat: item.bomAuditStat });
        }
      });
      boomApplyAudit(params).then(res => {
        this.$notify.success('物料审核成功');
        this.resetAudit();
        this.$emit('boomApplyAuditSuccess');
      }).catch(er => {});
    },
    resetAudit() {
      this.remake = null;
      this.showList.forEach((item) => {
        if (item.bomAuditStat) {
          delete item.bomAuditStat;
        }
      });
      this.showList = [];
      this.dialogVisible = false;
    }
  }
};
</script>
<style lang="scss" scoped>
>>> .el-dialog{
  margin-top: 5vh !important;
}
.title{
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  line-height: 22px;
  color: #666666;
  span{
    display: inline-block;
  }
}
.purpose{
  margin: 0;
  padding: 0;
  margin-bottom: 20px;
}
.clinicName{
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  line-height: 26px;
  color: #333333;
}
ul{
  padding: 0 0 10px;
  margin: 0;
  li{
    list-style: none;
    height: 50px;
    line-height: 50px;
    position: relative;
    img{
      vertical-align: middle;
      margin: 0 10px;
      cursor: pointer;
    }
  }
}
.el-divider{
  margin: 15px 0;
}
.showCount{
  display: inline-block;
  color: #707070;
  font-family: Microsoft YaHei;
}
.showCount:nth-child(1){
  width: 100px;
}
.showCount:nth-child(3){
  width: 150px;
  color: #999999;
  font-size: 12px;
}
.showCount:last-child{
  width: 50px;
  text-align: center;
}
>>> .radio-change{
  position: absolute;
  right: 20px;
  top: 20px;
}
.btn{
  margin-top: 20px;
  text-align: right;
}
</style>
